package com.mce.nebula.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mce.nebula.common.BaseEntity;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据中心配置实体，对应Python的RegionConfig模型
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "region_info",
       uniqueConstraints = {
           @UniqueConstraint(columnNames = "region_id"),
           @UniqueConstraint(columnNames = "region_name")
       })
public class RegionConfig extends BaseEntity {
    
    @Column(name = "region_id", length = 36, unique = true, nullable = false)
    private String regionId;
    
    @Column(name = "region_name", length = 64, unique = true, nullable = false)
    private String regionName;
    
    @Column(name = "region_alias", length = 64, nullable = false)
    private String regionAlias;
    
    @Column(name = "region_type", length = 64)
    private String regionType; // JSON array stored as string
    
    @Column(name = "url", length = 256, nullable = false)
    private String url;
    
    @Column(name = "wsurl", length = 256, nullable = false)
    private String wsurl;
    
    @Column(name = "httpdomain", length = 256, nullable = false)
    private String httpdomain;
    
    @Column(name = "tcpdomain", length = 256, nullable = false)
    private String tcpdomain;
    
    @Column(name = "token", length = 255)
    private String token;
    
    @Column(name = "status", length = 2, nullable = false)
    private String status; // 0:编辑中 1:启用 2:停用 3:维护中
    
    @Column(name = "description", length = 200)
    private String description;
    
    @Column(name = "scope", length = 10)
    private String scope = "private"; // private|public
    
    @Column(name = "ssl_ca_cert", columnDefinition = "TEXT")
    private String sslCaCert;
    
    @Column(name = "cert_file", columnDefinition = "TEXT")
    private String certFile;
    
    @Column(name = "key_file", columnDefinition = "TEXT")
    private String keyFile;
    
    @Column(name = "enterprise_id", length = 36)
    private String enterpriseId;
    
    @Column(name = "provider", length = 24)
    private String provider;
    
    @Column(name = "provider_cluster_id", length = 64)
    private String providerClusterId;
    
    /**
     * 获取数据中心类型列表
     */
    public List<String> getRegionTypeList() {
        if (regionType != null) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                return mapper.readValue(regionType, List.class);
            } catch (JsonProcessingException e) {
                log.warn("Failed to parse region_type", e);
            }
        }
        return new ArrayList<>();
    }
    
    /**
     * 设置数据中心类型列表
     */
    public void setRegionTypeList(List<String> types) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            this.regionType = mapper.writeValueAsString(types);
        } catch (JsonProcessingException e) {
            log.warn("Failed to serialize region types", e);
            this.regionType = "[]";
        }
    }
}